package com.atguigu.gulimall.ware.service.impl;

import com.atguigu.gulimall.ware.constant.WareConstant;
import com.atguigu.gulimall.ware.entity.WmsPurchaseDetailEntity;
import com.atguigu.gulimall.ware.service.WmsPurchaseDetailService;
import com.atguigu.gulimall.ware.service.WmsWareSkuService;
import com.atguigu.gulimall.ware.vo.PurchaseDoneVo;
import com.atguigu.gulimall.ware.vo.PurchaseItemDoneVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.ware.dao.WmsPurchaseDao;
import com.atguigu.gulimall.ware.entity.WmsPurchaseEntity;
import com.atguigu.gulimall.ware.service.WmsPurchaseService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("wmsPurchaseService")
public class WmsPurchaseServiceImpl extends ServiceImpl<WmsPurchaseDao, WmsPurchaseEntity> implements WmsPurchaseService {
    @Resource
    private WmsPurchaseDetailService purchaseDetailService;
    @Resource
    private WmsWareSkuService wareSkuService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WmsPurchaseEntity> page = this.page(
                new Query<WmsPurchaseEntity>().getPage(params),
                new QueryWrapper<WmsPurchaseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<WmsPurchaseEntity> unreceiveList() {
        QueryWrapper<WmsPurchaseEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("status", 4).orderByDesc();
        return this.list(queryWrapper);
    }

    @Override
    public void merge(List<Long> detailIds, Long purchaseId) {
        QueryWrapper<WmsPurchaseDetailEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", detailIds);
        List<WmsPurchaseDetailEntity> list = purchaseDetailService.list(queryWrapper);
        list.forEach(purchaseDetail -> {
            purchaseDetail.setPurchaseId(purchaseId);
            purchaseDetailService.updateById(purchaseDetail);
        });
    }

    @Override
    @Transactional
    public void received(List<Long> ids) {
        //获取状态为新建或已分配的采购单 并将状态改为领取
        List<WmsPurchaseEntity> purchaseEntityList = ids.stream().map(id -> {
            WmsPurchaseEntity byId = this.getById(id);
            return byId;
        }).filter(purchase ->
                purchase.getStatus() == WareConstant.PurchaseStatusEnum.CREATED.getCode() || purchase.getStatus() == WareConstant.PurchaseStatusEnum.ASSIGNED.getCode()
        ).map(item -> {
            item.setStatus(WareConstant.PurchaseStatusEnum.RECEIVE.getCode());
            return item;
        }).collect(Collectors.toList());

        this.updateBatchById(purchaseEntityList);

        ids.forEach(id -> {
            QueryWrapper<WmsPurchaseDetailEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("purchase_id", id);
            List<WmsPurchaseDetailEntity> list = purchaseDetailService.list(queryWrapper);
            final List<WmsPurchaseDetailEntity> purchaseDetailList = list.stream().map(purchaseDetail -> {
                purchaseDetail.setStatus(WareConstant.PurchaseDetailStatusEnum.BUYING.getCode());
                return purchaseDetail;
            }).collect(Collectors.toList());
            purchaseDetailService.updateBatchById(purchaseDetailList);
        });


    }

    @Override
    @Transactional
    public void done(PurchaseDoneVo doneVo) {
        //改变采购单状态
        WmsPurchaseEntity purchaseEntity = this.getById(doneVo.getId());
        Boolean flag = true;
        List<WmsPurchaseDetailEntity> purchaseDetailEntityList = new ArrayList<>();
        // 2.改变采购项状态
        for (PurchaseItemDoneVo purchaseItemDoneVo : doneVo.getItems()) {
            WmsPurchaseDetailEntity byId = purchaseDetailService.getById(purchaseItemDoneVo.getItemId());
            if (purchaseItemDoneVo.getStatus() == WareConstant.PurchaseDetailStatusEnum.HASERROR.getCode()) {
                flag = false;
                byId.setStatus(purchaseItemDoneVo.getStatus());
            } else {
                byId.setStatus(WareConstant.PurchaseDetailStatusEnum.FINISH.getCode());
                //将采购需求入库
                wareSkuService.addStock(byId.getSkuId(), byId.getWareId(), byId.getSkuNum());
            }
            purchaseDetailEntityList.add(byId);
        }
        purchaseDetailService.updateBatchById(purchaseDetailEntityList);
        // 对采购单的状态进行更新
        WmsPurchaseEntity wmsPurchaseEntity = new WmsPurchaseEntity();
        wmsPurchaseEntity.setId(doneVo.getId());
        wmsPurchaseEntity.setStatus(flag ? WareConstant.PurchaseStatusEnum.FINISH.getCode() : WareConstant.PurchaseStatusEnum.HASERROR.getCode());
        wmsPurchaseEntity.setUpdateTime(new Date());
        this.updateById(wmsPurchaseEntity);
    }



}